package com.zpf.dao.Impl;

import com.zpf.dao.UserDao;
import com.zpf.domain.User;
import com.zpf.util.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    private QueryRunner queryRunner = new QueryRunner();

    @Override
    public User select(String username, String password) {
        Connection connection = DataSourceUtils.getConnection();
        String sql = "select id,username,PASSWORD,realname,email,gender,flag,role from user where username=? and PASSWORD = ?";
        try {
           return queryRunner.query(connection,sql,new BeanHandler<>(User.class),username,password);
        } catch (SQLException e) {
         throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }
}
